from flask import request
from sqlalchemy import or_

from apps.a_common.constants import SEARCH_MAX_SIZE
from apps.a_common.request import get_param
from apps.users.models import User, Permission


def filter_search_user():
    keyword = get_param(request.args, 'keyword', str)
    return User.query.filter(or_(User.username.ilike(f'%{keyword}%'),
                                 User.first_name.ilike(f'%{keyword}%'),
                                 User.last_name.ilike(f'%{keyword}%'))).limit(SEARCH_MAX_SIZE)


def filter_search_permission():
    keyword = get_param(request.args, 'keyword', str)
    return Permission.query.filter(Permission.name.ilike(f'%{keyword}%')).limit(SEARCH_MAX_SIZE)
